package controller.admin;

import model.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminUserResetServlet", value = "/admin/user_reset")
public class AdminUserResetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService us = new UserService();
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        
        if (!(id != null && !"".equals(id) && password != null && !"".equals(password))) {
            request.setAttribute("failMsg", "参数错误！");
            request.getRequestDispatcher("user_list").forward(request, response);
            return;
        }
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setPassword(password);

        boolean flag = us.updatePwd(user);
        if (flag) {
            request.setAttribute("msg", "修改成功！");
        } else {
            request.setAttribute("failMsg", "修改失败！");
        }
        request.getRequestDispatcher("user_list").forward(request, response);
    }
}
